<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="患者id" prop="patientId">
        <el-input
            v-model="queryParams.patientId"
            placeholder="请输入患者id"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>


      <el-form-item label="治疗时间" prop="cureTime">
        <el-date-picker clearable
                        v-model="queryParams.cureTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择治疗时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['masterdocker:cure:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['masterdocker:cure:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['masterdocker:cure:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['masterdocker:cure:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cureList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="员工id" align="center" prop="doctorId" />
      <el-table-column label="患者id" align="center" prop="patientId" />
      <el-table-column label="患者名字" align="center" prop="doctorId" />
      <el-table-column label="住院登记表id" align="center" prop="registrationId" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['masterdocker:cure:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['masterdocker:cure:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

  </div>
</template>

<script setup name="Cure">
import {listCure, getCure, delCure, addCure, updateCure, listCurepatient} from "@/api/masterdocker/cure";
import {onMounted} from "vue";
import {getDocterId, listMasterdockerlog1} from "@/api/masterdocker/masterdockerlog.js";

const { proxy } = getCurrentInstance();

const cureList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const userId=localStorage.getItem("userId");
let dockerid=ref(0);//登录获取医生的id
const query=ref({
  id:userId
})
const query1=computed(() => ({
  doctorId: dockerid.value
}));
// 获取含该医生病人的总数据
let patienttotal=ref({});
// 获取病人的关键数据
let patientArr=ref([]);



const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    registrationId:null,
    admissionTime:null,
    doctorId: null,
    patientId: null

  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询治疗方案列表 */
function getList() {
  loading.value = true;
  listCurepatient(queryParams.value).then(response => {
    cureList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    console.log(cureList.value);
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    registrationId:null,
    admissionTime:null,
    patientId: null
  };
  proxy.resetForm("cureRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.cureId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加治疗方案";
}
onMounted(() => {

  getDocterId(query.value).then(response => {
    console.log(response);
    dockerid.value=response.myuserId;
    queryParams.value={
      pageNum: 1,
      pageSize: 10,
      recordTime: null,
      patientId: null,
      conditionChange: null,
      doctorId: response.myuserId,
      registrationId: null,
    };
    console.log(dockerid.value);
    console.log(query1.value);
    getList();
    listMasterdockerlog1(query1.value).then(response => {
      patienttotal.value=response.rows;
      patientArr.value=patienttotal.value.map(item => {
        return {
          id: item.patientId,
          name: item.patient.realName,
          registrationId:item.registrationId
        };
      });

      console.log(patienttotal.value);
      console.log(patientArr.value);

    })


  });


})







getList();
</script>
